//
//  PHDashLineView.m
//  SmallElephant
//
//  Created by Faith on 2024/11/25.
//

#import "PHDashLineView.h"

@implementation PHDashLineView {
    CGFloat _lineWidth;
    UIColor  *_color;
}

- (void)addDashedBorderToView:(UIColor *)color andLineWidth:(CGFloat)lineWidth {
    _lineWidth = lineWidth;
    _color = color;
    CAShapeLayer *dashedBorder = [CAShapeLayer layer];
    dashedBorder.strokeColor = color.CGColor; // 边框颜色
    dashedBorder.fillColor = [UIColor clearColor].CGColor; // 填充色透明
    dashedBorder.lineWidth = lineWidth; // 线宽
    dashedBorder.lineDashPattern = @[@2, @2]; // 虚线的线段和间隙长度（@4 表示线段长度，@2 表示间隙）
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(0, self.bounds.size.height)];
    
    // 底部虚线
    dashedBorder.path = path.CGPath;
    dashedBorder.frame = self.bounds;
    
    // 添加到视图的图层
    [self.layer addSublayer:dashedBorder];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self addDashedBorderToView:_color andLineWidth:_lineWidth];
}
@end
